package com.feldschmid.subdroid;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.feldschmid.subdroid.db.RevisionDbAdapter;
import com.feldschmid.subdroid.util.Action;
import com.feldschmid.subdroid.util.RevisionDataAdapter;

public class Revisions extends ListActivity {

	private static final int DEMO_ID = Menu.FIRST;

	private RevisionDbAdapter mDbHelper;
	private Long mRepositoryId;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.revisions);
		mDbHelper = new RevisionDbAdapter(this);
		mDbHelper.open();

		mRepositoryId = savedInstanceState != null ? savedInstanceState
				.getLong(RevisionDbAdapter.KEY_FK_REPID) : null;
		if (mRepositoryId == null) {
			Bundle extras = getIntent().getExtras();
			mRepositoryId = extras != null ? extras
					.getLong(RevisionDbAdapter.KEY_FK_REPID) : null;
		}

		fillData(mRepositoryId);
		registerForContextMenu(getListView());
	}
	
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        View filesView = v.findViewById(R.id.revision_files);
        filesView.setVisibility(filesView.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); 
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, DEMO_ID, 0, R.string.menu_demo);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case DEMO_ID:
			createDemoRevisions(mRepositoryId);
			fillData(mRepositoryId);
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void createDemoRevisions(Long repositoryId) {
		mDbHelper.createRevision(repositoryId, 0, Action.MODIFY, "afe",
				"2009-12-24", "Created SVN");
		mDbHelper.createRevision(repositoryId, 1, Action.MODIFY+Action.ADD, "afe",
				"2009-12-24", "first modify!");
		mDbHelper.createRevision(repositoryId, 11,  Action.MODIFY+Action.ADD+Action.DELETE, "afe",
				"2009-12-24", "copied files from old SVN");
		mDbHelper.createRevision(repositoryId, 100, Action.MODIFY+Action.ADD+Action.DELETE+Action.REPLACE, "afe",
				"2009-12-24", "deleted old notes");
		mDbHelper.createRevision(repositoryId, 1000, Action.MODIFY+Action.DELETE, "afe",
				"2009-12-24", "deleted old notes");
		mDbHelper.createRevision(repositoryId, 10010, Action.MODIFY+Action.REPLACE, "afe",
				"2009-12-24", "deleted old notes");
		mDbHelper.createRevision(repositoryId, 10020, Action.MODIFY+Action.DELETE+Action.REPLACE, "afe",
				"2009-12-24", "deleted old notes");
		mDbHelper.createRevision(repositoryId, 920300, Action.MODIFY+Action.DELETE, "AVeryLongAndUncommonName",
				"2009-12-24", "deleted old notes and wrote a very very very very long comment about it into the svn repository!\nEven added some linebreaks \n * And Bullet \n * Points");
	}

	private void fillData(Long mRepositoryId) {
		// Get all of the rows from the database and create the item list
		Cursor repoCursor = mDbHelper
				.fetchRevisionsForRepositoryId(mRepositoryId);
		startManagingCursor(repoCursor);

		// Create an array to specify the fields we want to display in the list
		String[] from = new String[] { RevisionDbAdapter.KEY_REVISION,
				RevisionDbAdapter.KEY_ACTION, RevisionDbAdapter.KEY_AUTHOR,
				RevisionDbAdapter.KEY_MESSAGE };

		// and an array of the fields we want to bind those fields to
		int[] to = new int[] { R.id.revision_revision, R.id.revision_actions,
				R.id.revision_author, R.id.revision_message };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter notes = new RevisionDataAdapter(this,
				R.layout.revision_row, repoCursor, from, to);
		setListAdapter(notes);
	}
}